package com.jinxiangyang.controller;


import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Notice;
import com.jinxiangyang.domain.vo.NoticeVo;
import com.jinxiangyang.service.NoticeService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "通知接口")
public class NoticeController {


    @Autowired
    private NoticeService noticeService;


    /**
     * 分页查询通知
     * @param pageNumber
     * @param pageSize
     * @param notice
     * @return
     */

    @GetMapping("/{pageNumber}/{pageSize}")
    @ApiOperation(value = "分页查询通知")
    @OperateLog(title = "查询通知模块",operatorType = OperatorType.OTHER, businessType = BusinessType.INSERT)
    public AjaxResult getNoticeByPage(@PathVariable Integer pageNumber,@PathVariable Integer pageSize,Notice notice){
        try {
            Map<String, Object> map = noticeService.selectNoticeAll(pageNumber,pageSize,notice);
            return AjaxResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }


    /**
     * 保存通知
     * @param notice
     * @return
     */


    @PostMapping
    @ApiOperation(value = "保存通知")
    @OperateLog(title = "查询通知模块",operatorType = OperatorType.OTHER, businessType = BusinessType.INSERT)
    public AjaxResult saveNotice(@RequestBody Notice notice){
        try {
            if (noticeService.save(notice)) {
              return AjaxResult.success();
            } else {
                AjaxResult.error("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.error("添加失败");
    }


    /**
     * 更新通知
     * @param notice
     * @return
     */

    @PutMapping
    @ApiOperation(value = "更新通知")
    @OperateLog(title = "查询通知模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.UPDATE)
    public AjaxResult updateNotice(@RequestBody Notice notice){
        try {
            if (Boolean.TRUE.equals(noticeService.updateNoticeById(notice))) {
                return AjaxResult.success();
            } else {
                AjaxResult.error("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("更新失败");
        }
        return AjaxResult.error("更新失败");
    }

    /**
     * 根据id删除通知
     * @param id
     * @return
     */

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除通知")
    @OperateLog(title = "查询通知模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.DELETE)
    public AjaxResult deleteNotice(@PathVariable Integer id){
        try {
            if (noticeService.removeById(id)) {
                return AjaxResult.success();
            } else {
                AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.error("删除失败");
    }
}

